package org.light.simpleauth;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.Util;
import org.light.exception.ValidateException;
import org.light.utils.DomainUtil;
import org.light.utils.WriteableUtil;

public class LoginInterceptorGenerator extends Util{
	protected Domain userDomain;
	public LoginInterceptorGenerator(){
		super();
		super.fileName = "LoginInterceptor.go";
	}
	
	public LoginInterceptorGenerator(Domain userDomain){
		super();
		this.setPackageToken(userDomain.getPackageToken());
		this.userDomain = userDomain;
		super.fileName = "LoginInterceptor.go";
	}

	@Override
	public String generateUtilString() throws ValidateException{
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package interceptor"));
		sList.add(new Statement(2000L,0,""));

		sList.add(new Statement(3000L,0,"import ("));
		sList.add(new Statement(4000L,1,"\"net/http\""));
		sList.add(new Statement(5000L,1,"\""+DomainUtil.packagetokenToGoPackageFolder(this.userDomain.getProjectName(),this.userDomain.getPackageToken()+"."+this.userDomain.getServiceimplSuffix())+"\""));
		sList.add(new Statement(6000L,0,""));
		sList.add(new Statement(7000L,1,"session \"github.com/ScottHuangZL/gin-jwt-session\""));
		sList.add(new Statement(8000L,1,"\"github.com/gin-gonic/gin\""));
		sList.add(new Statement(9000L,1,"\"fmt\""));
		sList.add(new Statement(10000L,1,"\"math/rand\""));
		sList.add(new Statement(11000L,0,")"));
		sList.add(new Statement(12000L,0,""));
		sList.add(new Statement(13000L,0,"func LoginInterceptor() gin.HandlerFunc {"));
		sList.add(new Statement(14000L,0,""));
		sList.add(new Statement(15000L,1,"return func(c *gin.Context) {"));
		sList.add(new Statement(16000L,2,"username, err := session.ValidateJWTToken(c)"));
		sList.add(new Statement(17000L,0,""));
		sList.add(new Statement(18000L,2,"loginFlag := false"));
		sList.add(new Statement(19000L,2,"if err == nil && username != \"\" {"));
		sList.add(new Statement(20000L,3,"loginFlag = "+this.userDomain.getServiceimplSuffix()+".CanAccess(username,c.Request.URL.Path)"));
		sList.add(new Statement(21000L,3,"//loginFlag = true"));
		sList.add(new Statement(22000L,2,"}"));
		sList.add(new Statement(23000L,2,"if loginFlag == false {"));
		sList.add(new Statement(24000L,3,"myrand := fmt.Sprintln(rand.Intn(1000000))"));
		sList.add(new Statement(25000L,3,"//session.GetFlashes(c)"));
		sList.add(new Statement(26000L,3,"//session.DeleteAllSession(c)"));
		sList.add(new Statement(27000L,3,"//c.Redirect(http.StatusMovedPermanently,\"/login/index.html\")"));
		sList.add(new Statement(28000L,3,"c.Redirect(http.StatusTemporaryRedirect, \"/login/noauth.html?rand=\"+myrand)"));
		sList.add(new Statement(29000L,3,"c.Abort()"));
		sList.add(new Statement(30000L,3,"return"));
		sList.add(new Statement(31000L,2,"}"));
		sList.add(new Statement(32000L,2,"c.Next()"));
		sList.add(new Statement(33000L,1,"}"));
		sList.add(new Statement(34000L,0,"}"));


		return WriteableUtil.merge(sList).getContent();
	}

	public Domain getUserDomain() {
		return userDomain;
	}

	public void setUserDomain(Domain userDomain) {
		this.userDomain = userDomain;
	}

}
